<!-- 报销申请 -->
<template>
  <!-- 用户搜索卡片 -->
  <el-card style="margin: 20px;height: 140px;">
    <el-form :inline="true">
      <el-form-item label="申请人" style="width: 270px;">
        <el-input placeholder="请输入申请人"  v-model="searchApplication.payeeName"
                   @keydown.enter="loadReimbursement"></el-input>
      </el-form-item>
      <el-form-item label="申请部门" style="width: 290px;">
        <el-select placeholder="请选择部门" v-model="searchApplication.department"
                   @change="loadReimbursement">
          <el-option label="研发部" value="研发部"></el-option>
          <el-option label="市场部" value="市场部"></el-option>
          <el-option label="财务部" value="财务部"></el-option>
          <el-option label="销售部" value="销售部"></el-option>
          <el-option label="人力资源部" value="人力资源部"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="申请状态" style="width: 290px;">
        <el-select placeholder="请选择状态"  v-model="searchApplication.statusReimbursemen"
                   @change="loadReimbursement">
          <el-option label="待审批" value="0"></el-option>
          <el-option label="审批通过" value="1"></el-option>
          <el-option label="审批拒绝" value="2"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item style="float: right">
        <el-row type="flex" justify="end" style="width: 100%;">
          <el-col>
            <el-button type="primary" @click="loadReimbursement" style="display:block; width:100%; margin-left: 10px;">搜索</el-button>
            <el-button @click="reset" style="display:block; width:100%; margin-top: 10px;">重置</el-button>
            <el-button type="primary" style=" display:block; width:100%; margin-top: 10px;"
                       @click="beforeAdd" >新增申请</el-button>
          </el-col>
        </el-row>
      </el-form-item>
      <el-form-item style="float: right">

      </el-form-item>
    </el-form>
  </el-card>

<!-- 新增弹窗(根据显示用户) -->
  <el-dialog :title="dialogTitle" v-model="addApplicationDialogVisible"
             :before-close="handleClose" style="width:1000px;padding:40px;">
    <el-form label-width="80px" label-position="top">
      <el-row :gutter="30">
        <el-col :span="12">
          <el-form-item label="申请人">
            <el-input placeholder="请输入" v-model="saveApplicationForm.payeeName"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="申请部门">
            <el-select placeholder="请选择部门" v-model="saveApplicationForm.department">
              <el-option label="研发部" value="研发部"></el-option>
              <el-option label="市场部" value="市场部"></el-option>
              <el-option label="财务部" value="财务部"></el-option>
              <el-option label="销售部" value="销售部"></el-option>
              <el-option label="人力资源部" value="人力资源部"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="30">
        <el-col :span="12">
          <el-form-item label="申请日期">
            <el-date-picker
                type="datetime"
                start-placeholder="开始日期"
                value-format="YYYY-MM-DD HH:mm:ss"
                format="YYYY-MM-DD HH:mm:ss"
                v-model="createTime"
            ></el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="收款人全称">
            <el-input placeholder="请输入" v-model="saveApplicationForm.payeeName"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="30">
        <el-col :span="12">
          <el-form-item label="报销金额">
            <el-input placeholder="请输入" v-model="saveApplicationForm.amount"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="银行账户">
            <el-input placeholder="请输入" v-model="saveApplicationForm.bankAccount"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="30">
        <el-col :span="12">
          <el-form-item label="开户行">
            <el-input placeholder="请输入" v-model="saveApplicationForm.bankName"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="报销科目">
            <el-select placeholder="请选择"  v-model="saveApplicationForm.expenseCategory">
              <el-option label="差旅费" value="差旅费"></el-option>
              <el-option label="办公用品" value="办公用品"></el-option>
              <el-option label="交通费" value="交通费"></el-option>
              <el-option label="业务招待费" value="业务招待费"></el-option>
              <el-option label="培训费" value="培训费"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="30">
        <el-col :span="12">
          <el-form-item label="开票申请">
            <el-radio-group v-model="saveApplicationForm.invoiceRequested">
              <el-radio border label="是" :value="1" style="margin:0;"></el-radio>
              <el-radio border label="否" :value="0"></el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="纳税号">
            <el-input placeholder="请输入" v-model="saveApplicationForm.taxNumber"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <template #footer>
      <el-button @click="handleClose">取消</el-button>
      <el-button type="primary" @click="addApplication">确定</el-button>
    </template>
  </el-dialog>

<!--查看详细弹窗-->
  <el-dialog :title="dialogTitleT" v-model="addApplicationDialogVisibleT"
             :before-close="handleClose" style="width:1000px;padding:40px;">
    <el-form label-width="80px" label-position="top">
      <el-row :gutter="30">
        <el-col :span="12">
          <el-form-item label="申请人">
            <el-input placeholder="请输入" v-model="saveApplicationForm.payeeName"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="申请部门">
            <el-input placeholder="请输入" v-model="saveApplicationForm.department"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="30">
        <el-col :span="12">
          <el-form-item label="申请日期">
            <el-date-picker
                type="datetime"
                start-placeholder="开始日期"
                value-format="YYYY-MM-DD HH:mm:ss"
                format="YYYY-MM-DD HH:mm:ss"
                v-model="createTime"
            ></el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="收款人全称">
            <el-input placeholder="请输入" v-model="saveApplicationForm.payeeName"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="30">
        <el-col :span="12">
          <el-form-item label="报销金额">
            <el-input placeholder="请输入" v-model="saveApplicationForm.amount"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="银行账户">
            <el-input placeholder="请输入" v-model="saveApplicationForm.bankAccount"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="30">
        <el-col :span="12">
          <el-form-item label="开户行">
            <el-input placeholder="请输入" v-model="saveApplicationForm.bankName"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="报销科目">
            <el-select placeholder="请选择"  v-model="saveApplicationForm.expenseCategory">
              <el-option label="差旅费" value="差旅费"></el-option>
              <el-option label="办公用品" value="办公用品"></el-option>
              <el-option label="交通费" value="交通费"></el-option>
              <el-option label="业务招待费" value="业务招待费"></el-option>
              <el-option label="培训费" value="培训费"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="30">
        <el-col :span="12">
          <el-form-item label="开票申请">
            <el-radio-group v-model="saveApplicationForm.invoiceRequested">
              <el-radio border label="是" :value="1" style="margin:0;"></el-radio>
              <el-radio border label="否" :value="0"></el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="纳税号">
            <el-input placeholder="请输入" v-model="saveApplicationForm.taxNumber"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <template #footer>
    </template>
  </el-dialog>

<!-- 报销申请表数据 -->
  <el-card style="margin:20px;">
    <el-table :data="tableData">
      <el-table-column label="申请人" prop="payeeName" align="center"></el-table-column>
      <el-table-column label="所属部门" prop="department" align="center"></el-table-column>
      <el-table-column label="报销金额" prop="amount" align="center"></el-table-column>
      <el-table-column label="创建日期" prop="createTime" align="center"></el-table-column>
      <el-table-column label="申请状态" prop="statusReimbursemen" align="center"
                       :formatter="appStatusFormatter"></el-table-column>
      <el-table-column label="操作" align="center">
        <template #default="scope">
          <el-button type="primary" link
                     @click="viewDetails(scope.row.id)">查看详细</el-button>
          <el-button type="primary" link :disabled="scope.row.statusReimbursemen != '0'"
                     @click="cancelApplication(scope.row.id)">撤销</el-button>
        </template>
      </el-table-column>


    </el-table>
  </el-card>
</template>

<script setup>
import {onMounted, ref} from "vue";
import axios from "axios";
import {ElMessage} from "element-plus";
import qs from "qs";

//定义弹窗
const addApplicationDialogVisible = ref(false);
const addApplicationDialogVisibleT = ref(false);

// //获取当前登录的用户信息todo全局变量
const user = ref(getUser());

//定义弹窗标题
const dialogTitle = ref("创建申请单")
const dialogTitleT = ref("查看详细")
//定义变量用来保存表单中的"申请时间"
const createTime = ref('');
//保存报销申请的数据
const saveApplicationForm = ref({
  payeeName:'',
  department:'',
  createTime:'',
  amount:'',
  bankAccount:'',
  bankName:'',
  expenseCategory:'',
  invoiceRequested:'',
  taxNumber:'',
  statusReimbursemen:''
});
//新增报销申请单前
const beforeAdd = ()=>{
  saveApplicationForm.value = {};
  addApplicationDialogVisible.value = true;
}
//保存报销申请单
const addApplication = ()=>{
  saveApplicationForm.value.createTime = createTime.value;
  saveApplicationForm.value.statusReimbursemen = '0';
  let data = qs.stringify(saveApplicationForm.value);
  axios.post(BASE_URL + '/v1/finance/reimbursement/save',data).then((response)=>{
    if (response.data.code == 2000) {
      ElMessage.success("保存成功");
      addApplicationDialogVisible.value = false;
      loadReimbursement();
    }else{
      ElMessage.error(response.data.msg);
    }
  })
}
//取消报销申请单
const handleClose = ()=>{
  saveApplicationForm.value = {};
  addApplicationDialogVisible.value = false;
  addApplicationDialogVisibleT.value = false;
}

//存储搜索条件
const searchApplication = ref({
  payeeName:'',
  department:'',
  statusReimbursemen:''
})
//存储查询的报销申请的数据
const tableData = ref([]);
//定义查询报销申请数据的方法
const loadReimbursement = ()=> {
  let data = qs.stringify(searchApplication.value)
  axios.get(BASE_URL + '/v1/finance/reimbursement/select?'+ data).then((response)=>{
    if (response.data.code == 2000) {
    tableData.value = response.data.data;
  }else{
    ElMessage.error(response.data.msg);
  }
})
}
onMounted(()=>{
  loadReimbursement();
})
//重置搜索
const reset = ()=> {
  searchApplication.value = {};
  loadReimbursement();
}
//转化申请状态 数字=>文字
const appStatusFormatter = (row, column, cellValue, index)=>{
  if(cellValue ==  "0"){
    cellValue = '待审批';
  }
  if(cellValue ==  "1"){
    cellValue = '审批通过';
  }
  if(cellValue ==  "2"){
    cellValue = '审批拒绝';
  }
  return cellValue;
}

//定义查看详细的方法
const viewDetails = (id)=> {
  addApplicationDialogVisibleT.value = true;
  axios.get(BASE_URL+'/v1/finance/reimbursement/select?id='+id).then((response)=>{
    if(response.data.code == 2000){
      saveApplicationForm.value = response.data.data[0];
      createTime.value = response.data.data[0].createTime;
      console.log('回显数据')
      console.log(saveApplicationForm.value)
      addApplicationDialogVisibleT.value = true;
    }else {
      ElMessage.error(response.data.msg);
    }
  })
}

//定义撤销的方法
const cancelApplication = (id)=> {
  if (confirm('是否要撤销')){
    axios.post(BASE_URL+'/v1/finance/reimbursement/cancel/'+id).then((response)=> {
      if (response.data.code == 2000) {
        ElMessage.success('撤销成功');
        loadReimbursement();
      } else {
        ElMessage.error(response.data.msg);
      }
    })
  }
}





</script>

<style scoped>

</style>